<?php
require_once('inc/init.php');
require_once('inc/editor/ubb.php');
$tid=numFilter($_GET['tid']);
$action=strFilter($_GET["action"]);
$action=empty($action)? "view" : $action;
switch($action){
	case "view":
		$voterow=$db->row_select_one("votes","tid={$tid}");
		if($voterow['isopenvote']==0){
			exit("该投票非公开。");
		}

		$rows=$db->row_select("voteitems","tid={$tid}",0,"*","id");
		foreach($rows as $row){
			if(stristr(",{$row['voteuids']},", ",{$lg['userid']},")){
				$hasvote=true;
				break;
			}
		}
		if($voterow['userid']!=$lg['userid'] && $voterow['viewresult']==1&& $hasvote==false){
			exit("您还没有投票，不能查看结果。");
		}
		$itemoptions='';
		$usersdiv='';
		foreach($rows as $row){
			$row["title"]=htmlFilter($row["title"]);
			$voteuids=$row["voteuids"];
			$itemoptions.="<option value=\"{$row['id']}\">{$row['title']}</option>";
			$usersdiv.="<div id=\"div_{$row['id']}\" class=\"vote_div\" style=\"display:none;\">";
			if(!empty($voteuids)){
				$voteuids=(substr($voteuids, -1)==","?substr($voteuids,0,strlen($voteuids)-1):$voteuids);
				$userrows=$db->row_select("users","id in ({$voteuids})",0,"id,username","username");
				if(!empty($userrows)){
					foreach($userrows as $user){
						$usersdiv.="<p><a href=\"".genUrl("user.php?uid={$user['id']}")."\" target=\"_blank\" class=\"commonlink\">{$user['username']}</a></p>";
					}
				}else{
					$usersdiv.="<p>无</p>";	
				}
			}else{
				$usersdiv.="<p>无</p>";	
			}
			$usersdiv.="</div>";	
		}		
		require_once getTemplatePath('viewvote.htm');
	break;
}
?>